#!usr/bin/env python3

class SocketManager(object):
    def __init__(self):
        self.sockNum = 0 #记录当前在线的总数
        self.socketIndex = 1 #socketid ,自动递增不会减少
        self.client2Id = {} #通过客户端对象查找id的字典
        self.id2Client = {} #通过id查找客户端对象的字典
        
    def addClient(self,client):
        self.sockNum+=1
        self.client2Id[client] = self.socketIndex #记录id
        self.id2Client[self.socketIndex] = client #记录client
        print("add client id=",self.socketIndex,"current num=",self.sockNum)
        self.socketIndex+=1
        
        
        
    def delClient(self,client):
        if client in self.client2Id:
            self.sockNum-=1
            index = self.client2Id[client]
            del self.client2Id[client]
            del self.id2Client[index]
            print("del client id=",index,"current num=",self.sockNum)
    
    def getSocketId(self,client):
        if client in self.client2Id:
            return self.client2Id[client]
        return None
        
    def getClient(self,id):
        if id in self.id2Client:
            return self.id2Client[id]
        return None

sockManager = SocketManager() #初始化管理器
           